﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.DataHub
{
    /// <summary>
    /// 门诊主索引服务
    /// </summary>
    public class PatnMasterIdxService : DataHubAppService, IPatnMasterIdxService
    {
        /// <summary>
        /// 
        /// </summary>
        private IRepository<PatnMasterIdx, Guid> _patnMasterIdxRepository;
        private readonly PatnMasterIdxManager _patnBasInfoManager;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectConfigRepository"></param>
        public PatnMasterIdxService(IRepository<PatnMasterIdx, Guid> patnMasterIdxRepository
            , PatnMasterIdxManager patnBasInfoManager)
        {
            _patnMasterIdxRepository = patnMasterIdxRepository;
            _patnBasInfoManager = patnBasInfoManager;
        }

        /// <summary>
        /// 获取患者主索引
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<PatnMasterIdxDto>> GetListAsync(PatnMasterIdxInput input)
        {
            var iQuery = await _patnMasterIdxRepository.GetQueryableAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<PatnMasterIdx>, List<PatnMasterIdxDto>>(iQuery.ToList());

                return new PagedResultDto<PatnMasterIdxDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<PatnMasterIdx>, List<PatnMasterIdxDto>>(items.ToList());

                return new PagedResultDto<PatnMasterIdxDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 获取患者主索引明细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PatnMasterIdxDto> GetAsync(Guid id)
        {
            var patnBasInfo = await _patnMasterIdxRepository.GetAsync(id);
            var itemDto = ObjectMapper.Map<PatnMasterIdx, PatnMasterIdxDto>(patnBasInfo);

            return itemDto;
        }

        /// <summary>
        /// 修改患者主索引信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PatnMasterIdxDto> PutAsync(Guid id, PatnMasterIdxUpdateDto input)
        {
            var clinicPatient = await _patnMasterIdxRepository.GetAsync(id);
            ObjectMapper.Map(input, clinicPatient);
            clinicPatient = await _patnMasterIdxRepository.UpdateAsync(clinicPatient);
            return ObjectMapper.Map<PatnMasterIdx, PatnMasterIdxDto>(clinicPatient);
        }

        /// <summary>
        /// 创建患者主索引信息
        /// </summary>
        /// <returns></returns>
        public async Task<PatnMasterIdxDto> PostAsync(PatnMasterIdxCreateDto input)
        {
            PatnMasterIdx patnMasterIdx = null;

            if (input.MasterIdxType == EnumMasterIdxType.身份证号)
            {
                patnMasterIdx = _patnBasInfoManager.CreatePatnMasterIdx(input.MaterIdxValue, input.PatnName);
            }
            else
            {
                patnMasterIdx = _patnBasInfoManager.CreatePatnMasterIdx(input.MasterIdxType
                , input.MaterIdxValue, input.PatnName, input.Gend, input.Brdy);
            }

            patnMasterIdx = await _patnMasterIdxRepository.InsertAsync(patnMasterIdx);
            var itemDto = ObjectMapper.Map<PatnMasterIdx, PatnMasterIdxDto>(patnMasterIdx);

            return itemDto;
        }

        public async Task DeleteAsync(Guid id)
        { 
            await _patnMasterIdxRepository.DeleteAsync(id);
        }
    }
}
